﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CodeGenerator.interfaces;
namespace CodeGenerator.Implementations
{
    class GeneratorModel : IGeneratorModel
    {
        private CodeGenerator _codeGenerator;
        private IDatabaseProvider _databaseProvider;
        private  string _outputFolder;
        private IList<IGeneratorClass> _classes = new List<IGeneratorClass>();
        private string _nameSpace ="";
        private DatabaseEngines _databaseEngine;
        public IList<IGeneratorClass> Classes
        {
            get { return _classes;}
        }

        public IDatabaseProvider DatabaseProvider
        {
            get { return _databaseProvider; }
        }

        public string NameSpace
        {
            get { return _nameSpace; }
        }

 
              
        public GeneratorModel(string NameSpace, DatabaseEngines DatabaseEngine)
        {
            _nameSpace = NameSpace;
            _databaseEngine = DatabaseEngine;
        }

        public void LoadModel(IDatabaseProvider DatabaseProvider, string OutputFolder)
        {
            _databaseProvider = DatabaseProvider;
            _outputFolder = OutputFolder;
            _codeGenerator = new CodeGenerator(this, OutputFolder, _databaseEngine);
            LoadClassesFromProvider();
        }
        
        private void LoadClassesFromProvider()
        {
            _classes = _databaseProvider.GetTables();
            _codeGenerator.GenerateClassesSourceCode();
        }

        public void SaveSourceCode(bool ProjectFileGenerationEnabled, bool ProjectSampleGenerationEnabled)
        {
            
            _codeGenerator.SaveClassesSourceCode();
            _codeGenerator.SaveDataConnector();

            if (ProjectFileGenerationEnabled)
                _codeGenerator.SaveProjectSourceCode();

            if (ProjectSampleGenerationEnabled)
                _codeGenerator.SaveProjectSampleSourceCode(); 

            if(_databaseEngine == DatabaseEngines.SqlServer)
            {
                //Generate stored procedures
                _codeGenerator.SaveStoredProcedures();
            }

            //GenerateProjectSampleSourceCode
        }
    }

    
}
